<template>
  <div class="app-container">
    <NormalForm pageTitle="通知公告" :operateForm="queryForm" :formList="formList" :enumOpt="enumOpt" @handleQuery="handleQuery" @handleReset="handleReset" />
    <div class="g-card" style="padding: 0 20px 20px 20px">
      <div class="right-btn" style="margin-right: 0;">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['system:notice:add']">新增</el-button>
        <el-button icon="el-icon-delete" :disabled="currentSelectList.length == 0" @click="handleDelete" v-hasPermi="['system:notice:remove']">删除</el-button>
      </div>
      <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="60" align="center" />
        <el-table-column label="序号" align="center" prop="noticeId" min-width="60" />
        <el-table-column label="公告标题" align="center" prop="noticeTitle" :show-overflow-tooltip="true" />
        <el-table-column label="公告类型" align="center" prop="noticeType" min-width="100">
          <template slot-scope="scope">
            <span>{{ noticeTypeOpt[scope.row.noticeType] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" min-width="100">
          <template slot-scope="scope">
            <span>{{ statusOpt[scope.row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建者" align="center" prop="createBy" min-width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" min-width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="140">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:notice:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:notice:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
    </div>
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="updateVisible" width="780px" append-to-body :show-close="false" :close-on-click-modal="false">
      <el-form ref="updateForm" :model="updateForm" :rules="updateRules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input v-model="updateForm.noticeTitle" placeholder="请输入公告标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <el-select v-model="updateForm.noticeType" placeholder="请选择">
                <el-option v-for="dict in enumOpt.noticeTypeList" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="updateForm.status">
                <el-radio v-for="item in enumOpt.statusList" :key="item.dictValue" :label="item.dictValue">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="noticeContent">
              <Editor v-model="updateForm.noticeContent" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateSubmit">确 定</el-button>
        <el-button @click="updateCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";
import Editor from "@/components/Editor";

export default {
  name: "Notice",
  components: { Editor },
  data() {
    return {
      queryForm: {
        noticeTitle: null,
        createBy: null,
        noticeType: null,
        pageNum: 1,
        pageSize: 20,
      },
      formList: [
        {
          label: '公告标题',
          value: '',
          prop: 'noticeTitle',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '操作人员',
          value: '',
          prop: 'createBy',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '公告类型',
          value: '',
          prop: 'noticeType',
          type: 'SELECT',
          emunName: 'noticeTypeList'
        },
      ],
      enumOpt: {
        noticeTypeList: [],
        statusList: []
      },
      noticeTypeOpt: {},
      statusOpt: {},
      loading: true,
      total: 0,
      noticeList: [],
      currentSelectList: [],
      title: "",
      updateVisible: false,
      updateForm: {
        noticeTitle: null,
        noticeType: null,
        status: '0',
        noticeContent: null,
      },
      updateRules: {
        noticeTitle: [{ required: true, message: "公告标题不能为空", trigger: "blur" }],
        noticeType: [{ required: true, message: "公告类型不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getEnumListFn();
    this.getTableListFn();
  },
  methods: {
    getEnumListFn() {
      this.getDicts("sys_notice_status").then((res) => {
        if (res.code == 200) {
          this.enumOpt.statusList = res.data;
          res.data.forEach(item => {
            this.statusOpt[item.dictValue] = item.dictLabel;
          })
        }
      });
      this.getDicts("sys_notice_type").then((res) => {
        if (res.code == 200) {
          this.enumOpt.noticeTypeList = res.data;
          res.data.forEach(item => {
            this.noticeTypeOpt[item.dictValue] = item.dictLabel;
          })
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getTableListFn();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryForm = {
        noticeTitle: null,
        createBy: null,
        noticeType: null,
        pageNum: 1,
        pageSize: 20,
      };
      this.getTableListFn();
    },
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.getTableListFn()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.getTableListFn()
    },
    /** 查询公告列表 */
    getTableListFn() {
      this.loading = true;
      listNotice(this.queryForm).then((res) => {
        this.noticeList = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      });
    },
    // 取消按钮
    updateCancel() {
      this.updateVisible = false;
      this.$refs.updateForm.resetFields();
      this.updateForm = {
        noticeTitle: null,
        noticeType: null,
        status: '0',
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.currentSelectList = selection.map((item) => item.noticeId);
    },
    /** 新增按钮操作 */
    handleAdd() {
      console.log(this.updateForm)
      this.updateVisible = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const noticeId = row.noticeId || this.currentSelectList;
      getNotice(noticeId).then((response) => {
        this.updateForm = response.data;
        this.updateVisible = true;
        this.title = "修改公告";
      });
    },
    /** 提交按钮 */
    updateSubmit() {
      this.$refs.updateForm.validate((valid) => {
        if (valid) {
          if (this.updateForm.noticeId) {
            updateNotice(this.updateForm).then((response) => {
              if (response.code === 200) {
                this.$message.success("修改成功");
                this.updateCancel();
                this.getTableListFn();
              }
            });
          } else {
            addNotice(this.updateForm).then((response) => {
              if (response.code === 200) {
                this.$message.success("新增成功");
                this.updateCancel();
                this.getTableListFn();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.currentSelectList;
      this.$confirm(
        '是否确认删除公告编号为"' + noticeIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delNotice(noticeIds);
        })
        .then(() => {
          this.getTableListFn();
          this.$message.success("删除成功");
        })
        .catch(function () { });
    },
  },
};
</script>

<style  lang="scss" scoped>
@import "../../../styles/base.scss";

.fatherBox {
  display: flex;
  justify-content: space-between;
}
</style>